/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.rukopisyimport.foxml;

import cz.mzk.kramerius.commons.DatastreamId;
import cz.mzk.kramerius.commons.Namespaces;
import cz.mzk.kramerius.commons.PropertyLoader;
import cz.mzk.kramerius.imports.Datastream;
import cz.mzk.kramerius.imports.DatastreamFactory;
import cz.mzk.kramerius.imports.FoxmlHelper;
import cz.mzk.kramerius.imports.Policy;
import cz.mzk.kramerius.rukopisyimport.foxml.images.ImageUrlBuilder;
import cz.mzk.kramerius.rukopisyimport.sourceData.BarCodeResolver;
import cz.mzk.kramerius.rukopisyimport.sourceData.ManuscriptIdentifier;
import cz.mzk.kramerius.rukopisyimport.sourceData.MarcDocument;
import cz.mzk.kramerius.rukopisyimport.sourceData.TeiDocument;
import java.io.File;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.Node;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class ImageServerPageBuilder extends AbstractPageBuilder {

    private static final String PROP_SYSNO_BARCODE_MAPPING = "BarCodeResolver.mapping";
    private final ImageUrlBuilder imageUrlBuilder;
    private final BarCodeResolver barCodeResolver;

    public ImageServerPageBuilder(Properties properties) throws Exception {
        imageUrlBuilder = new ImageUrlBuilder(properties);
        PropertyLoader loader = new PropertyLoader(properties);
        File mapping = loader.loadFile(PROP_SYSNO_BARCODE_MAPPING, true, true, false);
        barCodeResolver = new BarCodeResolver(mapping);
    }

    @Override
    void createDatastreams(File conversionDir, Page page, Node surfaceNode, MarcDocument marc, TeiDocument tei) throws Exception {
        Datastream dc = createDcDatastream(page);
        page.addDatastream(dc);
        Datastream mods = createModsDatastream(page);
        page.addDatastream(mods);
        ManuscriptIdentifier mnsId = ManuscriptIdentifier.instanceOf(marc, barCodeResolver);
        ImageIdentifier imageId = ImageIdentifier.instanceOf(mnsId, page);
        Datastream relsExt = createRelsExtDatastream(page, imageId);
        page.addDatastream(relsExt);
        Datastream imgFull = createImgFullDatastream(imageId);
        page.addDatastream(imgFull);
        Datastream imgThumb = createImgThumbDatastream(imageId);
        page.addDatastream(imgThumb);
        Datastream policy = FoxmlHelper.createPolicyDatastream(Policy.PUBLIC);
        page.addDatastream(policy);
        FoxmlHelper.saveFoxml(conversionDir, page);
    }

    private Datastream createRelsExtDatastream(Page page, ImageIdentifier imageId) {
        Document doc = FoxmlHelper.createRelsExtSkeleton(page, "page", Policy.PUBLIC);
        String url = imageUrlBuilder.buildTilesUrl(imageId);
        FoxmlHelper.addElementToRelsExt(doc, "tiles-url", Namespaces.kramerius, url);
        return DatastreamFactory.createInlineDatastream(doc, DatastreamId.RELS_EXT);
    }

    private Datastream createImgThumbDatastream(ImageIdentifier imageId) {
        String url = imageUrlBuilder.buildImgThumbUrl(imageId);
        return DatastreamFactory.createExternalyReferencedDs(url, DatastreamId.IMG_THUMB);
    }

    private Datastream createImgFullDatastream(ImageIdentifier imageId) {
        String url = imageUrlBuilder.buildImgFullUrl(imageId);
        return DatastreamFactory.createExternalyReferencedDs(url, DatastreamId.IMG_FULL);
    }
}
